package net.realsee.controller.wcs.warehouse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.warehouse.bo.WcsWarehouseLocationBo;
import net.realsee.domain.wcs.warehouse.vo.WcsWarehouseLocationVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.service.wcs.warehouse.IWcsWarehouseLocationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 库位
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/warehouseLocation")
public class WcsWarehouseLocationController extends BaseController {

    private final IWcsWarehouseLocationService iWcsWarehouseLocationService;

    /**
     * 查询库位列表
     */
    @SaCheckPermission("wcs:warehouseLocation:list")
    @GetMapping("/list")
    public TableDataInfo<WcsWarehouseLocationVo> list(WcsWarehouseLocationBo bo, PageQuery pageQuery) {
        return iWcsWarehouseLocationService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出库位列表
     */
    @SaCheckPermission("wcs:warehouseLocation:export")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsWarehouseLocationBo bo, HttpServletResponse response) {
        List<WcsWarehouseLocationVo> list = iWcsWarehouseLocationService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "库位", WcsWarehouseLocationVo.class, response);
    }

    /**
     * 获取库位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:warehouseLocation:query")
    @GetMapping("/{id}")
    public R<WcsWarehouseLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(iWcsWarehouseLocationService.selectVoById(id));
    }

    /**
     * 新增库位
     */
    @SaCheckPermission("wcs:warehouseLocation:add")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsWarehouseLocationBo bo) {
        return toAjax(iWcsWarehouseLocationService.insertByBo(bo));
    }

    /**
     * 修改库位
     */
    @SaCheckPermission("wcs:warehouseLocation:edit")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsWarehouseLocationBo bo) {
        return toAjax(iWcsWarehouseLocationService.updateByBo(bo));
    }

    /**
     * 删除库位
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:warehouseLocation:remove")
    @Log(title = "库位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsWarehouseLocationService.deleteWithValidByIds(Arrays.asList(ids)));
    }
}
